//
//  ShopcarCell.m
//  jang
//
//  Created by lee han on 16/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "ShopcarCell.h"
#import "UIImageView+WebCache.h"
#import "CRUserData.h"
#import "CommonView.h"

@implementation ShopcarCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _mContainView.layer.cornerRadius = 8;
    _mContainView.layer.masksToBounds = YES;
    _mContainView.layer.borderWidth = 1;
    _mContainView.layer.borderColor = [[UIColor groupTableViewBackgroundColor] CGColor];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)initWithData:(NSDictionary*)rdata productdata:(NSDictionary*)pdata{
    mReseverId = [rdata objectForKey:@"id"];
    
    NSString* urlstr = [URLIMAGE_PRE stringByAppendingString:[pdata objectForKey:@"path"]];
    [_mImageView sd_setImageWithURL:[NSURL URLWithString:urlstr] placeholderImage:[UIImage imageNamed:@"wenhao.png"]];
    
    _mTextName.text = [pdata objectForKey:@"pname"];
    _mTextPrice.text = [NSString stringWithFormat:@"￥%@", [rdata objectForKey:@"price"]];
    _mTextStandard.text = [rdata objectForKey:@"sname"];
    
    mOldCount = [[rdata objectForKey:@"count"] integerValue];
    _mTextCount.text = [rdata objectForKey:@"count"];
    
    [self setSelectButton:[rdata objectForKey:@"select"] != nil];
    
    _mStepper.minimumValue = 1;
    _mStepper.maximumValue = [[rdata objectForKey:@"displaycount"] integerValue];
    _mStepper.value = mOldCount;
    
    [_mStepper addTarget:self action:@selector(onStpperChange:) forControlEvents:UIControlEventValueChanged];
    
}

-(void) setSelectButton:(BOOL)isselect{
    mIsSelected = isselect;
    if(isselect){
        [_mButtonSelect setBackgroundColor:[UIColor redColor]];
        [_mButtonSelect setTitle:@"取消" forState:UIControlStateNormal];
    }
    else{
        [_mButtonSelect setBackgroundColor:[UIColor greenColor]];
        [_mButtonSelect setTitle:@"选择" forState:UIControlStateNormal];
    }
}

-(void)onStpperChange:(id)sender{
    NSInteger changeCount = _mStepper.value - mOldCount;
    
    
    NSMutableArray* selects = [[NSMutableArray alloc] init];
    NSMutableDictionary* oneselect = [[NSMutableDictionary alloc] init];
    [oneselect setValue:mReseverId forKey:@"id"];
    [oneselect setValue:[NSString stringWithFormat:@"%ld", changeCount] forKey:@"count"];
    [selects addObject:oneselect];
    
    NSMutableDictionary* senddata = [[NSMutableDictionary alloc] init];
    [senddata setValue:selects forKey:@"add"];
    
    [[network sharedInstance] startHttpRequest:self Data:senddata functionType:NetworkFunctionType_AddShopCar];
}

- (IBAction)onClickSelect:(id)sender {
    //
    [self setSelectButton:!mIsSelected];
    if(_mDelegate){
        [_mDelegate onClickSelect:mReseverId Selected:mIsSelected];
    }
}

- (IBAction)onClickDelete:(id)sender {
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 mReseverId,  @"id",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_DeleteFromShopCar];
}

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary*)response functionType:(NSInteger)type{
    if(type == NetworkFunctionType_AddShopCar){
        if([[response objectForKey:@"status"] integerValue] == 1){
            
            [[CRUserData sharedInstance] addShopcar:mReseverId count:[NSString stringWithFormat:@"%ld", (int)_mStepper.value - mOldCount]];
            
            mOldCount = _mStepper.value;
            _mTextCount.text = [NSString stringWithFormat:@"%ld", mOldCount];
            
            if(_mDelegate){
                [_mDelegate onCountChanged:mReseverId Count:mOldCount];
            }
        }
        else{
            [CommonView addAlertView:nil Text:@"操作失败"];
            _mStepper.value = mOldCount;
        }
    }
    else if(type == NetworkFunctionType_DeleteFromShopCar){
        if([[response objectForKey:@"status"] integerValue] == 1){
            [[CRUserData sharedInstance] removeShopcar:mReseverId];
            
            if(_mDelegate){
                [_mDelegate onDelected:mReseverId];
            }
        }
        else{
            [CommonView addAlertView:nil Text:@"操作失败"];
        }
    }
}
//end networkDelegate
@end
